import axios from 'axios'
import { AI_CHAT_API_BASE_URL } from '../utils/constants'
import { getToken } from '@/utils/auth'

const api = axios.create({
    baseURL: AI_CHAT_API_BASE_URL,
    timeout: 30000,
    headers: {
        'Content-Type': 'application/json'
    }
})

api.interceptors.request.use(
    (config) => {
        const token = getToken()
        if (token) {
            config.headers['Authorization'] = 'Bearer ' + token
        }
        return config
    },
    (error) => {
        return Promise.reject(error)
    }
)

api.interceptors.response.use(
    (res) => {
        return res
    },
    (error) => {
        console.error('Agent Config API 错误:', error)
        if (error.response?.data?.detail) {
            return Promise.reject(new Error(error.response.data.detail))
        }
        return Promise.reject(error)
    }
)

const BASE = '/api/agent-config'

export default {
    getConfigs() {
        return api.get(BASE)
    },

    getTools() {
        return api.get(`${BASE}/tools`)
    },

    createConfig(data) {
        return api.post(BASE, data)
    },

    updateConfig(id, data) {
        return api.put(`${BASE}/${id}`, data)
    },

    deleteConfig(id) {
        return api.delete(`${BASE}/${id}`)
    },

    activateConfig(id) {
        return api.post(`${BASE}/${id}/activate`)
    }
}
